package com.gxuwz.attendance.dao;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import com.google.gson.Gson;
import com.gxuwz.attendance.entity.College;
import com.gxuwz.attendance.vo.Page;
import net.sf.json.JSONArray;

@Repository("CollegeDao")
public class CollegeDao extends SessionFactoryDao{
	
	
	/**
	 * 添加学院信息
	 * @param college
	 * @return
	 */
	public boolean add(College college) {
		boolean flag = true;
		
		Session session = null;
		
		long timeMillis = System.currentTimeMillis();
		
		try {
			session = getSession();
			college.setCollegeNo(timeMillis + "");
			
			// 开启事务
			session.beginTransaction();
			session.save(college);
			session.getTransaction().commit();
		}catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		
		return flag;
	}
	
	
	
	/**
	 * 更新学院信息
	 * @param college
	 * @return
	 */
	public boolean update(College college) {
		boolean flag = true;
		Session session = null;
		try {
			session = getSession();
			// 开启事务
			session.beginTransaction();
			session.update(college);
			session.getTransaction().commit();
		}catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		
		return flag;
	}
	
	
	/**
	 * 批量删除学院信息
	 * @param ids
	 * @return
	 */
	public boolean delete(String ids) {
		
		JSONArray array = JSONArray.fromObject(ids);

		Session session = null;
		session = getSession();
		
		boolean flag = true;
		
		session.beginTransaction();
		try {
			
			for(int i = 0; i < array.size(); i++) {
				College college = new College();
				
				college.setCollegeNo(array.getJSONObject(i).getString("collegeNo"));
				
				session.delete(college);
			}
			session.getTransaction().commit();
			
		}catch(Exception e) {
			flag = false;
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally {
			session.close();
		}
		
		return flag;
	}
		
		
	
	
	/**
	 * 模糊查询学院信息分页
	 * @param college
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<College> list(College college, Page page){
		List<College> list = new ArrayList<College>();
		
		String hql = "from College ";
		
		if(college.getCollegeName() != null && !"".equals(college.getCollegeName())) {
			hql = "from College where collegeName like '%" + college.getCollegeName() + "%'";
		}
		
		list = getSession().createQuery(hql).list();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		// 保存本次查询的总数
		session.setAttribute("collegeSize", list.size());
		
		if(!list.isEmpty()) {
			if(page.getPage() * page.getRows() > list.size()) {
				list = list.subList(page.getOffset(), list.size());
			}else {
				list = list.subList(page.getOffset(), page.getPage() * page.getRows());
			}
		}
		
		return list;
	}
	
	/**
	 * 模糊查询学院信息
	 * @param collegeName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<College> findByName(String collegeName){
		List<College> list = new ArrayList<College>();
		
		String hql = "from College ";
		
		if(collegeName != null && !"".equals(collegeName)) {
			hql = "from College where collegeName like '%" + collegeName + "%'";
		}
		
		list = getSession().createQuery(hql).list();

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		session.setAttribute("collegeList", list);
		
		Gson gson = new Gson();
		
		session.setAttribute("JsonCollege", gson.toJson(list));
		
		return list;
	}
	
}
